package com.sugar.subscribe.course.module.business.dal.mysql.coachwallet;

import java.time.LocalDateTime;
import java.util.*;

import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sugar.subscribe.course.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.subscribe.course.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.subscribe.course.module.business.controller.app.coachwallet.vo.AppCoachProfitHistoryReqVO;
import com.sugar.subscribe.course.module.business.controller.app.coachwallet.vo.AppCoachProfitHistoryRespVO;
import com.sugar.subscribe.course.module.business.controller.app.coachwallet.vo.AppCoachProfitRespVO;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachwallet.CoachWalletDO;
import org.apache.ibatis.annotations.Mapper;
import com.sugar.subscribe.course.module.business.controller.admin.coachwallet.vo.*;
import org.apache.ibatis.annotations.Select;

/**
 * 教练钱包 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface CoachWalletMapper extends BaseMapperX<CoachWalletDO> {

    default PageResult<CoachWalletDO> selectPage(CoachWalletPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CoachWalletDO>()
                .eqIfPresent(CoachWalletDO::getCoachId, reqVO.getCoachId())
                .betweenIfPresent(CoachWalletDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CoachWalletDO::getId));
    }

    @Select("select " +
            " sum(tmp_tbl.count) as order_count, " +
            " max((case when tmp_tbl.profit_arrival_status = 0 then tmp_tbl.profit_amount else 0 end)) as profit_amount, " +
            " max((case when tmp_tbl.profit_arrival_status = 1 then tmp_tbl.profit_amount else 0 end)) as arrival_amount " +
            "from" +
            " (select" +
            "   bo.profit_arrival_status, sum(bo.profit_amount) profit_amount, count(1) count" +
            "  from business_order bo" +
            "  where bo.deleted = 0 and bo.status = 4 and bo.coach_id = #{param1} and bo.predict_profit_arrival_time between #{param2} and #{param3}" +
            "  group by bo.profit_arrival_status" +
            " ) tmp_tbl")
    AppCoachProfitRespVO statisticsCoachProfit(Long coachId, LocalDateTime beginTime, LocalDateTime endTime);
}